package org.feng.fxfront.util;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.scene.Cursor;
import javafx.scene.control.*;
import javafx.scene.paint.Color;
import org.feng.fxfront.pages.components.LogTextAreaComponent;

import java.util.function.Consumer;

/**
 * control工具类
 *
 * @version v1.0
 * @author: fengjinsong
 * @date: 2024年01月10日 22时01分
 */
public class ControlUtil {

    public static Button genDeleteButton(String text) {
        Button button = new Button(text);
        button.setMinWidth(36);
        button.setMinHeight(22);
        StyleUtil.setButtonBackground(button, Color.web("#FF6A6A"), Color.WHITE);
        return button;
    }

    public static Button genDeleteButton() {
        return genDeleteButton("删除");
    }

    public static Button genUpdateButton(String text) {
        Button button = new Button(text);
        button.setMinWidth(36);
        button.setMinHeight(22);
        StyleUtil.setButtonBackground(button, Color.web("#E9967A"), Color.WHITE);
        return button;
    }

    public static Button genSearchButton() {
        return genUpdateButton("搜索");
    }

    public static Button genClearButton() {
        return genUpdateButton("清空参数");
    }

    public static Button genUpdateButton() {
        return genUpdateButton("修改");
    }


    public static Button genAddButton(String text) {
        Button button = new Button(text);
        button.setMinWidth(36);
        button.setMinHeight(22);
        StyleUtil.setButtonBackground(button, Color.web("#1C86EE"), Color.WHITE);
        return button;
    }

    public static Button genAddButton() {
        return genAddButton("新增");
    }

    public static Button genClearLogButton() {
        return genDeleteButton("清空操作记录");
    }

    public static Button genRefreshButton() {
        return genAddButton("刷新列表");
    }

    public static Button pagingButton(String text) {
        Button button = new Button(text);
        button.setPrefSize(75, 22);
        button.setCursor(Cursor.HAND);
        return button;
    }

    public static Separator separator(double width) {
        Separator separator = new Separator();
        separator.setMaxWidth(width);
        separator.setHalignment(HPos.CENTER);
        return separator;
    }

    public static EventHandler<ActionEvent> alertHandler(Alert.AlertType type, String title, String headerText, String contentText, Consumer<ButtonType> buttonTypeConsumer) {
        return actionEvent -> {
            Alert alert = new Alert(type);
            alert.setTitle(title);
            alert.setHeaderText(headerText);
            alert.setContentText(contentText);
            alert.showAndWait().ifPresent(buttonTypeConsumer);
        };
    }

    public static EventHandler<ActionEvent> alertWarning(String headerText, String contentText, Consumer<ButtonType> buttonTypeConsumer) {
        return alertHandler(Alert.AlertType.WARNING, "操作警告", headerText, contentText, buttonTypeConsumer);
    }

    public static EventHandler<ActionEvent> alertInfo(String headerText, String contentText, Consumer<ButtonType> buttonTypeConsumer) {
        return alertHandler(Alert.AlertType.INFORMATION, "操作提示", headerText, contentText, buttonTypeConsumer);
    }

    public static EventHandler<ActionEvent> alertConfirm(String headerText, String contentText, Consumer<ButtonType> buttonTypeConsumer) {
        return alertHandler(Alert.AlertType.CONFIRMATION, "操作确认", headerText, contentText, buttonTypeConsumer);
    }

    public static EventHandler<ActionEvent> alertError(String headerText, String contentText, Consumer<ButtonType> buttonTypeConsumer) {
        return alertHandler(Alert.AlertType.ERROR, "操作错误", headerText, contentText, buttonTypeConsumer);
    }

    public static void refreshTableView(TableView<?> tableView, LogTextAreaComponent logger) {
        logger.log("正在刷新列表，请稍后...");
        tableView.refresh();
        logger.log("刷新列表完成！");
    }
}
